//
//  HotClassificationVC.m
//  jingdongfang
//
//  Created by MTiMac01 on 2017/8/25.
//  Copyright © 2017年 LJL. All rights reserved.
//

#import "HotClassificationVC.h"
#import "FilterReusableView.h"
#import "FilterCell.h"
#import "HotClassDetailVC.h"
#import "HotClassCell_first.h"
#import "HotClassCell_second.h"
#import "IGHotClassApi.h"

@interface HotClassificationVC ()<UICollectionViewDelegateFlowLayout, UICollectionViewDataSource, UICollectionViewDelegate>
@property (nonatomic, strong) IGHotClassApi *api;
@property (strong, nonatomic) NSMutableArray *dataSource;
@property (nonatomic, strong)UICollectionView *collectionV;
@end

@implementation HotClassificationVC

- (void)viewDidLoad
{
    [super viewDidLoad];
    [self creatNav];
    [self loadData];
    [self loadUI];
    [self dateRequest];
}

- (void)creatNav
{
//    self.title = @"分类";
    self.title = NSLocalizedString(@"Category", nil);
}

- (void)loadData
{
    self.api = [IGHotClassApi new];
    self.dataSource = [NSMutableArray array];
//    self.dataSource = @[@[@"油画",@"国画",@"摄影",@"版画",@"新媒体",@"水粉水彩",@"数码绘画",@"素描"], @[@"当代", @"近现代", @"古代"], @[@"人物", @"静物", @"风景", @"抽象", @"超现实", @"写实", @"人物"], @[@"文艺复兴", @"古典", @"新古典", @"抽象", @"超现实", @"写实"],@[@"黑白", @"红", @"黄", @"蓝", @"绿", @"紫"]];
//    titleArr = @[@"类别", @"年代", @"题材", @"艺术流派", @"色彩"];
}

- (void)dateRequest
{
    [self.api startWithDecodeClass:[IGHotClassResp class] CompletionBlockWithSuccess:^(id responseObject, NSError *error) {
        if (responseObject)
        {
            IGHotClassResp *resp = responseObject;
            IGHotClassModel *model = resp.data;
            self.dataSource = [NSMutableArray arrayWithArray:model.lstClass];
            [self.collectionV reloadData];
        }
        else
        {
            
        }
    }];
}

- (void)loadUI
{
    UICollectionViewFlowLayout * layout = [[UICollectionViewFlowLayout alloc] init];
//    layout.itemSize = CGSizeMake((self.view.width - 30)/3, 30);
    self.collectionV = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT) collectionViewLayout:layout];
    self.collectionV.backgroundColor = [UIColor c3Color];
    self.collectionV.delegate = self;
    self.collectionV.dataSource = self;
    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterCell" bundle:nil] forCellWithReuseIdentifier:@"FilterCell"];
    [self.collectionV registerNib:[UINib nibWithNibName:@"HotClassCell_first" bundle:nil] forCellWithReuseIdentifier:@"HotClassCell_first"];
    [self.collectionV registerNib:[UINib nibWithNibName:@"HotClassCell_second" bundle:nil] forCellWithReuseIdentifier:@"HotClassCell_second"];
    
    [self.collectionV registerNib:[UINib nibWithNibName:@"FilterReusableView" bundle:nil] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView"];
    [self.collectionV registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter"];
    [self.view addSubview:self.collectionV];
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    return self.dataSource.count ? self.dataSource.count : 0;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    IGHotClass *class = self.dataSource[section];
    return self.dataSource.count ? [class.lstSubClass count] : 0;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        IGHotClass *class = self.dataSource[indexPath.section];
        //有图
        if ([class.type isEqualToString:@"1"])
        {
            HotClassCell_first *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotClassCell_first" forIndexPath:indexPath];
//            IGHotClass *class = self.dataSource[indexPath.section];
            NSArray *lstClass = class.lstSubClass;
            cell.titleL.text = lstClass[indexPath.item][@"title"];
            [cell.imageV sd_setImageWithURL:[NSURL URLWithString:class.lstSubClass[indexPath.item][@"image"]]];
            return cell;
        }
        else if ([class.type isEqualToString:@"2"])
        {
            HotClassCell_second *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"HotClassCell_second" forIndexPath:indexPath];
//            IGHotClass *class = self.dataSource[indexPath.section];
            cell.textL.text = class.lstSubClass[indexPath.item][@"title"];
            return cell;
        }
    }
    return nil;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        IGHotClass *class = self.dataSource[indexPath.section];
        //有图
        if ([class.type isEqualToString:@"1"])
        {
            CGFloat wid = (self.view.width - 60) / 4;
            return CGSizeMake((self.view.width - 60) / 4, wid * 9 / 16 + 50);
        }
        else if ([class.type isEqualToString:@"2"])
        {
            return CGSizeMake((self.view.width - 60)/3, 35);
        }
    }
    return CGSizeZero;
}

- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader])
    {
        IGHotClass *class = self.dataSource[indexPath.section];
        FilterReusableView *reusableV = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"FilterReusableView" forIndexPath:indexPath];
        if (!reusableV)
        {
            reusableV = [[NSBundle mainBundle] loadNibNamed:@"FilterReusableView" owner:self options:nil].firstObject;
        }
        reusableV.typeL.text = self.dataSource.count > 0 ? class.title : @"";
        reusableV.moreBtn.hidden = YES;
        return reusableV;
    }
    UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"UICollectionElementKindSectionFooter" forIndexPath:indexPath];
    if (!footer)
    {
        footer = [[UICollectionReusableView alloc] init];
    }
    footer.backgroundColor = [UIColor c2Color];
    return footer;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(self.view.width, 50);
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    return CGSizeMake(self.view.width, 10);
}

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionVeliew layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        IGHotClass *class = self.dataSource[section];
        //有图
        if ([class.type isEqualToString:@"1"])
        {
            return UIEdgeInsetsMake(0, 15, 0, 15);
        }
        else if ([class.type isEqualToString:@"2"])
        {
            return UIEdgeInsetsMake(5, 20, 5, 20);
        }
    }
    return UIEdgeInsetsZero;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    IGHotClass *class = self.dataSource[indexPath.section];
    HotClassDetailVC *vc = [[HotClassDetailVC alloc] init];
    if (indexPath.section == 0)
    {
        vc.type = @"classType";
    }
    else if (indexPath.section == 1)
    {
        vc.type = @"years";
    }
    else if (indexPath.section == 2)
    {
        vc.type = @"theme";
    }
    else
    {
        vc.type = @"label";
    }
    vc.title = class.lstSubClass[indexPath.item][@"title"];
    vc.value = class.lstSubClass[indexPath.item][@"id"];
    [UIViewController ig_pushViewController:vc animated:YES];
}

//cell的最小列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        IGHotClass *class = self.dataSource[section];
        //有图
        if ([class.type isEqualToString:@"1"])
        {
            return 0;
        }
        else if ([class.type isEqualToString:@"2"])
        {
            return 10;
        }
    }
    return 0;
}

//cell的最小行间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (self.dataSource && self.dataSource.count > 0)
    {
        IGHotClass *class = self.dataSource[section];
        //有图
        if ([class.type isEqualToString:@"1"])
        {
            return 0;
        }
        else if ([class.type isEqualToString:@"2"])
        {
            return 5;
        }
    }
    return 0;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}


@end
